<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="dataForm.jobClassName" placeholder="任务类名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-if="checkPermission(['admin:quartz:list'])" @click="getDataList">查询</el-button>
        <el-button v-if="checkPermission(['admin:quartz:saveOrUpdate'])" type="primary" @click="addData">新增</el-button>
        <el-button v-if="checkPermission(['admin:quartzLog:list'])" type="success" @click="getLogList">日志列表</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="list"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"  v-loading="listLoading">
      <el-table-column align="center"
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column align="center"
        prop="jobClassName"
        label="任务类名">
      </el-table-column>
      <el-table-column align="center"
        prop="parameter"
        label="参数">
      </el-table-column>
      <el-table-column align="center"
        prop="description"
        label="描述">
      </el-table-column>
      <el-table-column
        prop="status" align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="success">运行</el-tag>
          <el-tag v-if="scope.row.status === 1" type="info">暂停</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="handleEdit(scope.row)" v-if="checkPermission(['admin:quartz:info'])">编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdateStatus(scope.row, 3)" v-if="checkPermission(['admin:quartz:remove'])">删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdateStatus(scope.row, 0)" v-if="checkPermission(['admin:quartz:stopJob'])">暂停</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdateStatus(scope.row, 1)" v-if="checkPermission(['admin:quartz:resumeJob'])">恢复</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdateStatus(scope.row, 2)" v-if="checkPermission(['admin:quartz:runJob'])">立即执行</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[20, 50, 100, 200]"
        :page-size="dataForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    <list-add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></list-add-or-update>
    <log v-if="logVisible" ref="log" @refreshDataList="getDataList"></log>
  </div>
</template>

<script>
  import quartzListApi from '@/api/quartz/list'
  import ListAddOrUpdate from './list-add-or-update.vue'
  import Log from './log.vue'
  export default {
    data () {
      return {
        dataForm: {
          page: 1,
          limit: 20,
          jobClassName: ''
        },
        multipleSelection: [],
        list: [],
        total: 0,
        addOrUpdateVisible: false,
        listLoading: false,
        logVisible: false
      }
    },
    components: {
      ListAddOrUpdate,
      Log
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 查询列表
      getDataList() {
        this.listLoading = true
        quartzListApi.listAll(this.dataForm).then(res => {
          this.list = res.data.pages.records
          this.total = res.data.pages.total
          this.listLoading = false
        }).catch(res => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      },
      // 每页多少条数据
      handleSizeChange(val) {
        this.dataForm.limit = val
      },
      // 当前第几页
      handleCurrentChange(val) {
        this.dataForm.page = val
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      // 新增
      addData() {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init()
        })
      },
      // 编辑
      handleEdit(row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row.id)
        })
      },
      // 删除
      handleDelete(row) {
        quartzListApi.removeById(row.id).then(res => {
          if (res.data.data) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getDataList()
          }
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      handleUpdateStatus(row, type) {
        var desc = ''
        if (type === 0) {
          desc = '暂停'
        } else if (type === 1) {
          desc = '恢复'
        } else if (type === 2) {
          desc = '立即执行'
        } else {
          desc = '删除'
        }
        this.$confirm(`确定进行${desc}操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (type === 0) {
            this.handleStop(row)
          } else if (type === 1) {
            this.handleResume(row)
          } else if (type === 2) {
            this.handleRun(row)
          } else {
            this.handleDelete(row)
          }
        }).catch(() => {
          this.$message('取消')
        })
      },
      // 暂停
      handleStop(row) {
        quartzListApi.stopWork(row.id).then(res => {
          if (res.data.data) {
            this.$message({
              message: '暂停成功',
              type: 'success'
            })
            this.getDataList()
          }
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      // 恢复
      handleResume(row) {
        quartzListApi.resumeWork(row.id).then(res => {
          if (res.data.data) {
            this.$message({
              message: '恢复成功',
              type: 'success'
            })
            this.getDataList()
          }
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      // 立即执行
      handleRun(row) {
        quartzListApi.runWork(row.id).then(res => {
          if (res.data.data) {
            this.$message({
              message: '立即执行成功',
              type: 'success'
            })
            this.getDataList()
          }
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      // 日志列表
      getLogList() {
        this.logVisible = true
        this.$nextTick(() => {
          this.$refs.log.init()
        })
      }
    }
  }
</script>

<style scoped></style>